﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class LoginControl : UserControl
    {
        private bool isLogin = false;
        private MainForm form;

        public LoginControl(MainForm mainForm)
        {
            InitializeComponent();

            form = mainForm;
        }

        public void AutoLogin()
        {

        }

        // 로그인 이벤트
        private void btnLogin_Click(object sender, EventArgs e)
        {
            CCL.SocketMessage.Request.Login packet = new CCL.SocketMessage.Request.Login(txtID.Text, txtPswd.Text);
            CCL.SocketMessage.Message message = CCL.SocketMessage.Message.MessageRequestBuilder(packet);
            byte[] msg = message.GetByte();

            /*
            string str = Connector.RecvMsg();
            if (null == str)
            {
                // Connector failed
                // 서버와의 연결이 안되어 있는 상태
                MessageBox.Show("서버와 연결이 되어있지 않습니다.");
                return;
            }

            string recvMsg = Connector.RecvMsg();*/

            CCL.SocketMessage.Message loginMsg = MainForm.connector.Execute(msg);
            CCL.SocketMessage.Response.Login loginResponse = (CCL.SocketMessage.Response.Login)loginMsg.Body;


            // 로그인 확인
            if (loginResponse.Result == CCL.SocketMessage.Constant.Result.Success)
            {
                // 유저정보를 입력함.

                MainForm.userData = loginResponse.UserInfo;
                isLogin = true;

                CCL.SocketMessage.Request.GetProjectList reqPacket = new CCL.SocketMessage.Request.GetProjectList(MainForm.userData.UserID);
                CCL.SocketMessage.Message plMessage = CCL.SocketMessage.Message.MessageRequestBuilder(reqPacket);
                CCL.SocketMessage.Message reqMsg = MainForm.connector.Execute(plMessage.GetByte());
                CCL.SocketMessage.Response.GetProjectList plResponse = (CCL.SocketMessage.Response.GetProjectList)reqMsg.Body;
                if (plResponse.Result == CCL.SocketMessage.Constant.Result.Success)
                {
                    MainForm.projList = plResponse.ProjectList;
                }

                // 자동 로그인을 체크했을 경우
                // 자동 로그인 레지스트리를 설정해줌.
                if (cbAutoLogin.Checked == true)
                {
                    //form.AutoLogin();
                }
                form.CheckLoginState();
                form.ChangeLoginMenu(true);

            }
            else
            {
                MessageBox.Show(CCL.Utility.StringUtility.ConvertToStr(loginResponse.ErrorMessage));
                isLogin = false;
            }
        }

        // 회원가입 이벤트
        private void btnJoin_Click(object sender, EventArgs e)
        {
            CreateAccountForm form = new CreateAccountForm();
            form.ShowDialog();
        }
    }
}
